#-*- encoding:gbk -*-
import wx
import SvnClient
import BoxConfig
import time

class TaskBarFrame(wx.Frame):
    
    def __init__(self, parent):
        
        wx.Frame.__init__(self, parent, style=wx.FRAME_NO_TASKBAR | wx.NO_FULL_REPAINT_ON_RESIZE)
        self.icon_state = False
        self.blink_state = False
        
        self.ID_ICON_TIMER = wx.NewId()
        
        self.config = BoxConfig.BoxConfig()
        self.URL = self.config.get_url()
        self.PATH = self.config.get_path()
        self.TIMETICK = self.config.get_tick()*1000
        self.USERNAME = self.config.get_username()
        self.PASSWORD = self.config.get_password()
        
        self.tbicon = wx.TaskBarIcon()
        icon = wx.Icon('yellow.ico', wx.BITMAP_TYPE_ICO)
        self.tbicon.SetIcon(icon, '')
        
        wx.EVT_TASKBAR_LEFT_DCLICK(self.tbicon, self.OnTaskBarLeftDClick)
        wx.EVT_TASKBAR_RIGHT_UP(self.tbicon, self.OnContextMenu)
        
        self.client = SvnClient.SvnClient(self.URL, self.PATH, self.USERNAME, self.PASSWORD)
        self.client.checkout()

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        self.timer.Start(self.TIMETICK)
       
        #self.Show(True)
    
    def OnTimer(self, evt):
        
        self.client = SvnClient.SvnClient(self.URL, self.PATH, self.USERNAME, self.PASSWORD)     
        self.client.check()
        self.client.update()
        
    def OnTaskBarLeftDClick(self, evt):
        try:
            self.icontimer.Stop()
        except:
            pass
        if self.icon_state:
            icon = wx.Icon('yellow.ico', wx.BITMAP_TYPE_ICO)
            self.tbicon.SetIcon(icon, 'Yellow')
            self.icon_state = False
        else:
            self.SetIconTimer()
            self.icon_state = True

    def OnRightDown(self, event):
        self.Destroy()
        self.tbicon.Destroy()
        self.Close(True)
        wx.GetApp().ProcessIdle()

    def SetIconTimer(self):
        self.icontimer = wx.Timer(self, self.ID_ICON_TIMER)
        wx.EVT_TIMER(self, self.ID_ICON_TIMER, self.BlinkIcon)
        self.icontimer.Start(1000)

    def BlinkIcon(self, evt):
        if not self.blink_state:
            icon = wx.Icon('red.ico', wx.BITMAP_TYPE_ICO)
            self.tbicon.SetIcon(icon, 'Red')
            self.blink_state = True
        else:
            icon = wx.Icon('black.ico', wx.BITMAP_TYPE_ICO)
            self.tbicon.SetIcon(icon, 'Black')
            self.blink_state = False

    def OnContextMenu(self, event):
        if not hasattr(self, "popupID1"):
            self.popupID1 = wx.NewId()
            self.Bind(wx.EVT_MENU, self.OnRightDown, id=self.popupID1)
        menu = wx.Menu()
        item = wx.MenuItem(menu, self.popupID1, "exit")
        menu.AppendItem(item)
        self.PopupMenu(menu)
        menu.Destroy()

class App(wx.App):
    """Application class."""
    
    def OnInit(self):
        self.frame = TaskBarFrame(None)
        self.frame.Show(False)            
        
        return True

def main():
    app = App(0)
    app.MainLoop()

if __name__ == '__main__':
    
    main()

        
    

